#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/7/24 14:58
# @Author  : ystraw
# @Site    : 
# @File    : main.py
# @Software: PyCharm Community Edition
# @function: 查词义并加至词库

import requests
import random
from bs4 import BeautifulSoup
import time
import random
import re
from urllib import parse
import database_tools
import random

def write(name, context):
    with open(name, 'w', encoding='utf-8') as f:
        f.write(context)
    print('已写入!')

#查询单词，无需登入
def search(word):
    # words = 'shared'
    ua_list = [
        "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
        "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36",
    ]
    agent = random.choice(ua_list)
    headers = {
        "Origin": "http://dict.kekenet.com",
        "Connection": "keep-alive",
        "Accept": "application/json, text/javascript, */*; q=0.01",
        "Referer": "http://dict.kekenet.com/index.php?do=login",
        "User-Agent": agent,
    }
    sess = requests.session()
    #查询连接：
    wd = parse.quote(word) #转码：
    url = 'http://dict.kekenet.com/en/' + word
    # print(url)
    response = sess.get(url, headers=headers).text

    bs = BeautifulSoup(response, 'lxml')
    # 提取翻译
    trans = bs.findAll('div', attrs={'id': 'wordm'} )
    # 提取例句
    sentence = bs.findAll('ul', attrs={'id': 's_ul'} )
    # 获取音标
    yb = bs.findAll('span', attrs={'class': 'phn'})
    # print('音标：', yb[0].string)

    moduel = re.compile('<.*?>')

    rt_info = ''
    rt_sentence = ''
    rt_yb = ''
    try:
        st = str(trans[0])
        rt_info = re.sub(moduel, '\n', st)
        rt_sentence = re.sub(re.compile(r"<(.*?)>", re.S), "", str(sentence[0]))
        rt_yb = yb[0].string
    except Exception:
        # print(trans)
        # print(Exception)
        pass
    return [rt_info, rt_sentence, rt_yb]

# 从文本获取每单元单词
def getWords(pathname, unit):
    # 获取单词
    print(pathname + str(unit) + '.txt')
    with open(pathname + str(unit) + '.txt', 'rb+') as f:
        string = f.read().decode("utf8", "ignore")
        # print(string)
    words = string.split('\n')
    # print(words)
    return words

if __name__ == '__main__':
    # 获取单词
    pathname = 'word/人教版五年级下册英语单词/unit'  # 读取单词路径
    bookname = '人教版五年级下册英语单词'            # 插入数据库的书名
    for unit in range(3, 7):
        words = getWords(pathname, unit)
        # print(words)

        # print(words)
        words_name = []
        words_mean = []
        tt = 0
        i = 0
        while i < len(words):
            word = words[i]
            word = word.replace('\r', '')
            print('++++++', word, '++++')
            if word != None and word != '':
                words_name.append(word)
                # 查询单词
                # word = 'articulately'
                try:
                    rt_word = search(word.lower())
                    # print(rt_word)
                    words_mean.append(rt_word[0])
                    # print('-------------')
                    database_tools.myInsert(word, rt_word, unit, bookname) # 插入数据库
                    time.sleep(random.uniform(1, 5))
                except Exception:
                    print(word, ' 查询单词，异常----')
                    time.sleep(15)
                    i -= 1
            i += 1
                # break
        # print("单词name: ", words_name)
        # print("单词mean: ", words_mean)
        print(len(words_name), len(words_mean))
        print('----unit' + str(unit) + '已完成！-----\n')
    print('程序正常结束！')